package com.cskaoyan.config;

import com.cskaoyan.config.realm.CustomRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.LinkedHashMap;

/**
 * @ClassName ShiroConfiguration
 * @Description
 * @Author Lxz
 * @Date 2022-8-16 8:51
 */

@Configuration
public class ShiroConfiguration {
    /**
     * ShiroFilter → SecurityManager
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();

        filterChainDefinitionMap.put("/admin/auth/login", "anon");

        filterChainDefinitionMap.put("/admin/auth/info", "anon");
        filterChainDefinitionMap.put("/admin/auth/noAuthc", "anon");
        filterChainDefinitionMap.put("/admin/user/list", "anon");
        filterChainDefinitionMap.put("/admin/**", "authc");

        /*
         * todo-Lxz 更新程序会导致重定向，无法退出登录
         * @Date  14:29
         **/
        shiroFilterFactoryBean.setLoginUrl("/admin/auth/login");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    /**
     * SecurityManager →
     * Authenticator 可有可无的
     * Authorizer 可有可无的
     * Realms
     * SessionManager
     */
    @Bean
    public DefaultWebSecurityManager securityManager(CustomRealm realm,
                                                     DefaultWebSessionManager sessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealms(Arrays.asList(realm));
        securityManager.setSessionManager(sessionManager);
        return securityManager;
    }

    /**
     * Realm extends AuthorizingRealm
     * 也可以直接增加@Component注册为容器中的组件
     */
    /*@Bean
    public AuthorizingRealm realm() {
        return new CustomRealm();
    }*/

    /**
     * SessionManager
     * 选择Shiro提供的，看包
     */
    @Bean
    public DefaultWebSessionManager sessionManager() {
        //return new DefaultWebSessionManager();
        return new MarketSessionManager();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }


}



